<?php
require_once "../class/fenajax.class.php";
include "../dyn/connexion.php";

$f = new fenAjax("Formulaire magique",0);

if(isset($_POST['mode']) and isset($_POST['id_carre']) and isset($_POST['mot']) and isset($_POST['nbLC']) and isset($_POST['nom']) and isset($_POST['position_mot']) and isset($_POST['contenu']))
{	
	
	$mode = $_POST['mode'];
	$id_carre = $_POST['id_carre'];
	$mot = $_POST['mot'];
	$nbLC = $_POST['nbLC'];
	$nom = $_POST['nom'];
	$position_mot = $_POST['position_mot'];
	$contenu = $_POST['contenu'];
	
	$id = $_SESSION['user_id'];
	
	
	if( !in_array($mode,array(1,2,3,4,5)))
		$f->ajaxErreur("Fonction non supportée","Ce mode n'est pas supporté pour ce module");
	
	if($mode == 1){						$header = "Ajouter un carre magique"; 		$nomAction = "Ajouter";		$disable = ""; }
	elseif($mode == 2 or $mode == 5){	$header = "Supprimer le carre magique"; 	$nomAction = "Supprimer"; 	$disable = ""; }
	elseif($mode == 3){ 				$header = "Saisissez votre texte"; 			$nomAction = "Valider"; 	$disable = ""; }
	elseif($mode == 4){ 				$header = "Choisissez votre carré magique"; $nomAction = "Valider"; 	$disable = ""; }
		

	
	$f->retourAjax["boutton"] = $nomAction;
	$f->retourAjax["header"] = $header;
	
	
	$form = "<div id=\"feedback\"></div><br />";		
	$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionMagique($(this));\">";
	
	
	
	if ($mode == 1) { // Ajout

		$form .= " <script type=\"text/javascript\">
					init_spinner();
				</script>
		";
		
		$form .= "<div class=\"center\">";
		$form .= BMInfo("Saisiser dans le contenu, les mots necéssaires au carré (nombre de mot = dimension au carré, séparer par des espaces)");
		$form .= "</div>";
		
		$form .= "<br /><label class=\"formInfos\" for=\"nom\">Nom :</label>
				<input class=\"ui-corner-all\" id=\"nom\" type=\"text\" value=\"".s($nom)."\" name=\"nom\" $disable/>
				<br />
		";
		
		$form .= "<br /><label class=\"formInfos\" for=\"nbLC\">Dimmension du carré :</label>
				<input id=\"nbLC\" type=\"text\" value=\"".s($nbLC)."\" name=\"nbLC\" $disable/>
				<br /><br />
		";
		
		$form .= "<label class=\"formInfos\" for=\"mot\">Contenu :</label>
					<textarea class=\"theme ui-corner-all\" name=\"mot\"></textarea>
		";
		
		$form .= "	<input type=\"hidden\" name=\"contenu\" value=\"".$contenu."\" />
					<input type=\"hidden\" name=\"id_carre\" value=\"".$id_carre."\" />
		";
	}
	
	if($mode == 2 or $mode == 4 or $mode == 5) { // Supprimer //Choisir carré //Supprimer User
		
		if ($mode == 2 or $mode == 4) {
			$res = mysql_query("SELECT * FROM CarreMagique;");
		}
		
		if ($mode == 5) {
		
			$form .= "<div class=\"center\">";
			$form .= BMInfo("Supprimer votre carré et recommencer un nouveau");
			$form .= "</div>";
			
			$res = mysql_query("SELECT * FROM CopieCarreMagique WHERE idUser = '".$id."';");
		}
		
		if (mysql_num_rows($res) != 0) {
			
			$form .= "<br /><label class=\"formInfos\" for=\"id_carre\">Carré magique :</label>
					<select id=\"id_carre\" name=\"id_carre\" $disable>
			";
			
			$prems = true;
			
			while($row = mysql_fetch_array($res)) {
				
				if ($mode == 2 or $mode == 4) {
					$form .= "<option value=\"".$row['id']."\" ".($prems ? "selected=\"selected\"":"").">".$row['nom']."</option>";
				}
				if ($mode == 5) {
					$form .= "<option value=\"".$row['idCarre']."\" ".($prems ? "selected=\"selected\"":"").">".$row['nom']."</option>";
				}
				$prems = false;
			}
			
			$form .= "</select>";
			
		}
		else {
			
			$form .= "<div class=\"center\">
						".BMInfo("Note: Il n'y a aucun carré magique de créer",false)."
					</div>
			";
			
			$form .= " <input type=\"hidden\" name=\"id_carre\" value=\"".$id_carre."\" />
			";

		}
		
		$form .= "	<input type=\"hidden\" name=\"contenu\" value=\"".$contenu."\" />
					<input type=\"hidden\" name=\"mot\" value=\"".$mot."\" />
					<input type=\"hidden\" name=\"nbLC\" value=\"".$nbLC."\" />
					<input type=\"hidden\" name=\"nom\" value=\"".$nom."\" />
		";
		
	}
	
	
		
	if ($mode == 3) { //Ajouter contenu
		$form .= "<br /><label class=\"formInfos\" for=\"contenu\">Votre texte:</label>
					<textarea class=\"theme ui-corner-all\" name=\"contenu\"></textarea><br /><br />";
		
		$form .= "<input type=\"hidden\" name=\"mot\" value=\"".$mot."\" />
					<input type=\"hidden\" name=\"nbLC\" value=\"".$nbLC."\" />
					<input type=\"hidden\" name=\"id_carre\" value=\"".$id_carre."\" />
					<input type=\"hidden\" name=\"nom\" value=\"".$nom."\" />
		";
	}
	
	
	
	$form .= "<input type=\"hidden\" name=\"position_mot\" value=\"".$position_mot."\" />
			<input type=\"hidden\" name=\"mode\" value=\"".$mode."\" />
	";
	
	$f->retourAjax["erreur"] = false;
	$f->retourAjax["msg"] = $form;
}

$f->endAjax();
?>